<template>
	<div class="system-company-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="700px" :close-on-click-modal="false">
			<el-form ref="companyDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="平台名称">
							<el-input v-model="state.ruleForm.cname" placeholder="必须小写或驼峰不能有特殊字符" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="Key">
							<el-input v-model="state.ruleForm.api_key" placeholder="请输入api_key" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="Appid">
							<el-input v-model="state.ruleForm.app_id" placeholder="请输入Appid" clearable></el-input>
						</el-form-item>
					</el-col>
					
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="秘钥">
							<el-input v-model="state.ruleForm.api_secret" placeholder="请输入秘钥" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="平台网址">
							<el-input v-model="state.ruleForm.url" placeholder="请输入平台网址" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="签名秘钥">
							<el-input v-model="state.ruleForm.secret" placeholder="请输入签名秘钥" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="使用网址">
							<el-input v-model="state.ruleForm.website" placeholder="请输入使用网址" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="多渠道">
							<el-select v-model="state.ruleForm.multi" filterable placeholder="请选择多渠道" clearable class="w100">
								<el-option v-for="(item,index) in state.platsData.palt" :key="item.id" :label="item.cname" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-switch v-model="state.ruleForm.status" :active-value="1" :inactive-value="0" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="隐藏链接">
							<el-switch v-model="state.ruleForm.hidden" :active-value="1" :inactive-value="0" inline-prompt active-text="隐藏" inactive-text="正常"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemCompanyDialog">
import { reactive, ref } from 'vue';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
import { platApi } from '/@/api/plat/index';
// 定义变量内容
const companyDialogFormRef = ref();
import { commApi } from '/@/api/comm/index';
const state = reactive({
	platsData:{
		palt:[],
	},
	ruleForm: {
		cname: '',
		api_key: '',
		api_secret: '',
		url: '',
		secret: '',
		app_id:'',
		multi:'',
		website:'',
		hidden: 1,
		status: 1, // 状态
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type, row) => {
	state.dialog.type = type
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.dialog.title = '修改平台';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增平台';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {
			cname: '',
			multi:'',
			api_key: '',
			app_id:'',
			api_secret: '',
			website:'',
			url: '',
			secret: '',
			hidden: 1,
			status: 1, // 状态
		}
	}
	getPlats()
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
const getPlats = () => {
	commApi().getPlats().then(res=>{
		state.platsData = res.data
	})
}
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') { 
		platApi().add(state.ruleForm).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}else{
		platApi().edit(state.ruleForm).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
